package org.example.httpserver.socketio;

import com.corundumstudio.socketio.SocketIOClient;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ClientCache {
    private static final Map<String, HashMap<UUID, SocketIOClient>> concurrentHashMap = new ConcurrentHashMap<>();

    public void saveClient(String userId,UUID sessionId, SocketIOClient client) {
        if(StringUtil.isNullOrEmpty(userId)){
            HashMap<UUID, SocketIOClient> clients = concurrentHashMap.get(userId);
            if(clients == null){
                clients = new HashMap<>();
            }
            clients.put(sessionId,client);
            concurrentHashMap.put(userId,clients);
        }

    }

    public HashMap<UUID, SocketIOClient> getUserClient(String userId) {
        return concurrentHashMap.get(userId);
    }

    public void deleteSessionClient(String userId,UUID sessionId) {
        concurrentHashMap.get(userId).remove(sessionId);
    }
}
